package com.db.williamchart.renderer.executor;

import com.db.williamchart.data.AxisType;
import com.db.williamchart.data.Paddings;

public class MeasureLineChartPaddings {

    public Paddings invoke(AxisType axisType, Float labelsHeight, Float longestLabelWidth, Float labelsPaddingToInnerChart, Float lineThickness, int pointsDrawableWidth, int pointsDrawableHeight) {
        Paddings labelsPaddings =
                measureLabelsPaddingsX(
                        axisType,
                        labelsHeight,
                        labelsPaddingToInnerChart
                ).mergeWith(
                        measureLabelsPaddingsY(
                                axisType,
                                labelsHeight,
                                longestLabelWidth,
                                labelsPaddingToInnerChart
                        )
                );

        return new Paddings(
                labelsPaddings.getLeft() + lineThickness / 2 + pointsDrawableWidth / 2,
                labelsPaddings.getTop() + lineThickness + pointsDrawableHeight / 2,
                labelsPaddings.getRight() + lineThickness / 2 + pointsDrawableWidth / 2,
                labelsPaddings.getBottom() + lineThickness + pointsDrawableHeight / 2
        );
    }

    private Paddings measureLabelsPaddingsX(
            AxisType axisType,
            Float labelsHeight,
            Float labelsPaddingToInnerChart
    ) {
        return new Paddings(
                0f,
                0f,
                0f,
                axisType.shouldDisplayAxisX() ? labelsHeight + labelsPaddingToInnerChart : 0F
        );
    }

    private Paddings measureLabelsPaddingsY(
            AxisType axisType,
            Float labelsHeight,
            Float longestLabelWidth,
            Float labelsPaddingToInnerChart
    ) {

        return !axisType.shouldDisplayAxisY() ?
                new Paddings(0f, 0f, 0f, 0f) :
                new Paddings(
                        longestLabelWidth + labelsPaddingToInnerChart,
                        labelsHeight / 2,
                        0F,
                        labelsHeight / 2
                );

    }
}
